//引入集合模型
require("../models/questionsModel");
require("../models/usersModel");
const { collectionsModel } = require("../models/collectionsModel");
const { errorQuestionsModel } = require("../models/errorQuestionsModel");

async function getAllError(req, res, next) {
    const { user_id } = req.body;
    //错题和收藏
    let allErrorArr = await errorQuestionsModel.find({ user_id }).populate('errorQuestions_id').populate('user_id');

    let allCollectionsArr = await collectionsModel.find({ user_id }).populate('questions_id').populate('user_id');
    res.send({ code: 1, allErrorArr, allCollectionsArr });
}
//学会错题
async function delError(req, res, next) {
    const { user_id, errorQuestions_id } = req.body;
    let count = await errorQuestionsModel.deleteMany({ errorQuestions_id, user_id });
    if (count.deletedCount > 0) {
        res.send({ code: 1, message: "学会错题" });
    } else {
        res.send({ code: 0, message: "学会错题失败" });
    }
}
//收藏错题
async function collectError(req, res, next) {
    const { user_id, questions_id } = req.body;

    let isHasArr = await collectionsModel.find({ user_id, questions_id }).populate('questions_id').populate('user_id');
    if (isHasArr.length == 0) {

        await collectionsModel.create({ user_id, questions_id });
        res.send({ code: 1, message: "收藏成功" });
    } else {

        res.send({ code: 0, message: "不能重复收藏" });
    }



}

module.exports = {
    //导出的函数名
    getAllError, delError, collectError
}